﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CBMS.Common;
using CBMS.Interfaces;
using CBMS.Business;
using CBMS.Models;
using CBMS.Resources; 

namespace CBMS.View
{
    public partial class frmMemberDetail : frmBase
    {
        #region Fields
        public static IMemberRepository memRep { get; set; }
        private int memberId = 0;
        private string strError = string.Empty;
        #endregion

        #region Contructor, Form_Load
        public frmMemberDetail()
        {
            InitializeComponent();
            memRep = new MemberRepository();
        }
        public frmMemberDetail(int _memberId)
        {
            memRep = new MemberRepository();
            this.memberId = _memberId; 
            InitializeComponent();
        }
        private void frmMemberDetail_Load(object sender, EventArgs e)
        {
            InitControl();
            SetObjectMember();
        }
        private Member GetObjectMember()
        {
            Member objMember = new Member();
            if (this.memberId > 0)
            {
                objMember = memRep.GetMemberById(this.memberId);
            } 
            objMember.member_type = cmbType.SelectedValue.ToString();
            objMember.last_name = txtLastName.Text.Trim();
            objMember.first_name = txtFirstName.Text.Trim();
            objMember.email = txtEmail.Text.Trim();
            objMember.phone = txtPhone.Text.Trim();
            objMember.birthday = txtDateOfBirth.Value;
            objMember.address = txtAddress.Text.Trim();
            objMember.suburb = txtSuburb.Text.Trim();
            objMember.state_code = cmbState.SelectedValue.ToString();
            objMember.country_code = cmbCountry.SelectedValue.ToString();
            objMember.zip = txtZip.Text.Trim();
            if (this.memberId == 0)
            {
                objMember.created_by = this.userLogin;
                objMember.created_date = CommonFunction.Now;
            }
            objMember.modified_by = this.userLogin;
            objMember.modified_date = CommonFunction.Now;
            return objMember;
        }
        private void SetObjectMember()
        {
            Member objMember = new Member();
            if (this.memberId > 0)
            {
                objMember = memRep.GetMemberById(this.memberId);
                txtMemnershipNumber.Text = objMember.membership_num;
                cmbType.SelectedValue = objMember.member_type;
                txtLastName.Text = objMember.last_name;
                txtFirstName.Text = objMember.first_name;
                txtEmail.Text = objMember.email;
                txtPhone.Text = objMember.phone;
                txtDateOfBirth.Value = objMember.birthday.Value;
                txtAddress.Text = objMember.address;
                txtSuburb.Text = objMember.suburb;
                cmbState.SelectedValue = objMember.state_code;
                cmbCountry.SelectedValue = objMember.country_code;
                txtZip.Text = objMember.zip;
            } 
        }
        private void ResetControl()
        {
            txtMemnershipNumber.Text = string.Empty;
            cmbType.SelectedValue = VALUES.DefautlValue.Code01;
            txtLastName.Text = string.Empty;
            txtFirstName.Text = string.Empty;
            txtEmail.Text = string.Empty;
            txtPhone.Text = string.Empty;
            txtAddress.Text = string.Empty;
            txtSuburb.Text = string.Empty;
            cmbState.SelectedValue = string.Empty;
            cmbCountry.SelectedValue = BaseFunctions.COUNTRY_Australia;
            txtZip.Text = string.Empty;
        }
        #endregion

        #region Methods
        private void InitControl()
        {
            CommonFunction.MakeComboBoxMaster(BaseFunctions.MEMBERSHIP_TYPEID, cmbType);
            cmbType.SelectedValue = VALUES.DefautlValue.Code01;
            CommonFunction.MakeComboBoxCountry(cmbCountry, true);
            cmbCountry.SelectedValue = BaseFunctions.COUNTRY_Australia;
            cmbCountry_SelectedIndexChanged(null, null);
            txtMemnershipNumber.Focus();
        }
        private bool CheckValidationForm()
        { 
            bool result = true;
            Control txt = new Control();
            string strNameText = string.Empty;
            string strAsign = "erro";
            string strErr = ErrorMessages.E0001;
            this.strError = string.Empty;
            if (cmbType.SelectedIndex == 0)
            {
                if (strNameText == string.Empty)
                {
                    strNameText = strAsign;
                    txt = (Control)cmbType;
                }
                this.strError += string.Format(ErrorMessages.E0002, BaseFunctions.Removetext(lblType.Text)) + "\r\n";
                result = false;
            }
            if (txtFirstName.Text.Trim() == string.Empty)
            {
                if (strNameText == string.Empty)
                {
                    strNameText = strAsign;
                    txt = (Control)txtFirstName;
                }
                this.strError += string.Format(strErr, BaseFunctions.Removetext(lblFirstName.Text)) + "\r\n";
                if (result)
                    result = false;
            }
            if (txtLastName.Text.Trim() == string.Empty)
            {
                if (strNameText == string.Empty)
                {
                    strNameText = strAsign;
                    txt = (Control)txtLastName;
                }
                this.strError += string.Format(strErr, BaseFunctions.Removetext(lblLastName.Text)) + "\r\n";
                if (result)
                    result = false;
            }
            if (txtEmail.Text.Trim() != string.Empty && !BaseFunctions.CheckFormatEmail(txtEmail.Text.Trim()))
            {
                if (strNameText == string.Empty)
                {
                    strNameText = strAsign;
                    txt = (Control)txtEmail;
                }
                this.strError += string.Format(ErrorMessages.E0003, BaseFunctions.Removetext(lblEmail.Text)) + "\r\n";
                if (result)
                    result = false;
            }
            if (txtAddress.Text.Trim() == string.Empty)
            {
                if (strNameText == string.Empty)
                {
                    strNameText = strAsign;
                    txt = (Control)txtAddress;
                }
                this.strError += string.Format(strErr, BaseFunctions.Removetext(lblAddress.Text)) + "\r\n";
                if (result)
                    result = false;
            }

            if (cmbState.SelectedIndex == 0 && cmbCountry.SelectedValue.ToString() == BaseFunctions.COUNTRY_Australia)
            {
                if (strNameText == string.Empty)
                {
                    strNameText = strAsign;
                    txt = (Control)cmbState;
                }
                this.strError += string.Format(ErrorMessages.E0002, BaseFunctions.Removetext(lblState.Text)) + "\r\n";
                if (result)
                    result = false;
            }
            //set focus at postition first error
            BaseFunctions.SetFocusWhenError(txt, result, strNameText);
            return result;
        }
        #endregion

        #region Handle Events
        private void btnSave_Click(object sender, EventArgs e)
        {
            bool boCkckInsert;
            boCkckInsert = CheckValidationForm();
            if (boCkckInsert)
            {
                var objMember = GetObjectMember(); 
                if (this.memberId == 0)
                {                    
                    objMember.membership_num = txtMemnershipNumber.Text.Trim();
                    //Check duplicate membership number
                    if (!string.IsNullOrEmpty(objMember.membership_num) && memRep.CheckDuplicateMembershipNumber(objMember.membership_num))
                    {
                        MessageBox.Show(string.Format(Resources.ErrorMessages.E0005, BaseFunctions.Removetext(lblMembershipNumber.Text)), ErrorMessages.T001, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        //Insert new member
                        memRep.Add(objMember);
                        memRep.SaveChanges();
                        MessageBox.Show(string.Format(ErrorMessages.I0001, "Insert"), ErrorMessages.T002, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.DialogResult = DialogResult.OK;
                    }
                }
                else
                {
                    //Check duplicate membership number
                    if (!string.IsNullOrEmpty(objMember.membership_num) && !objMember.membership_num.ToLower().Equals(txtMemnershipNumber.Text.Trim().ToLower()) && memRep.CheckDuplicateMembershipNumber(txtMemnershipNumber.Text.Trim()))
                    {
                        MessageBox.Show(string.Format(Resources.ErrorMessages.E0005, BaseFunctions.Removetext(lblMembershipNumber.Text)), ErrorMessages.T001, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        //Update member
                        objMember.membership_num = txtMemnershipNumber.Text.Trim();
                        memRep.SaveChanges();
                        MessageBox.Show(string.Format(ErrorMessages.I0001, "Update"), ErrorMessages.T002, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.DialogResult = DialogResult.OK;
                    }
                } 
            }
            else
            {
                MessageBox.Show(this.strError, ErrorMessages.T001, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            if (this.memberId > 0)
            {
                SetObjectMember();
            }
            else
            {
                ResetControl();
            }
        }

        private void cmbCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
            string country = cmbCountry.SelectedValue.ToString();
            CommonFunction.MakeComboBoxState(cmbState, country, true);
            if (country == BaseFunctions.COUNTRY_Australia)
            {
                cmbState.Enabled = true;
            }
            else
            {

                cmbState.Enabled = false;
            }
        }

        private void txtMemnershipNumber_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                btnSave_Click(null, null);
            }
        }

        private void txtFirstName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                btnSave_Click(null, null);
            }
        }

        private void txtLastName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                btnSave_Click(null, null);
            }
        }

        private void txtAddress_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                btnSave_Click(null, null);
            }
        }

        private void cmbState_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                btnSave_Click(null, null);
            }
        }
        #endregion    
    }
}